package EBM.UI;

import java.io.IOException;
import java.util.ArrayList;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import EBM.BLL.SocketConnection;
import EBM.BLL.Util;

public class SignIn extends org.eclipse.swt.widgets.Composite {
	private Label lblName;
	private Label lblPasswd;
	private Text txtName;
	private Button btnCancel;
	private Button btnEnter;
	private Text txtPasswd;
	public static Shell shell;
	public static Display display;
	public static String userType;
	
	MessageBox box = new MessageBox(shell, SWT.YES | SWT.ICON_ERROR);    

	/**
	* Auto-generated main method to display this 
	* org.eclipse.swt.widgets.Composite inside a new Shell.
	*/
	public static void main(String[] args) {
		showGUI();
	}
		
	/**
	* Auto-generated method to display this 
	* org.eclipse.swt.widgets.Composite inside a new Shell.
	*/
	public static void showGUI() {
	    display = Display.getDefault();
		shell = new Shell(display, SWT.MIN | SWT.CLOSE);
		SignIn inst = new SignIn(shell, SWT.NULL);
		Point size = inst.getSize();
		shell.setLayout(new FillLayout());
		shell.layout();
		shell.setText("登陆");
		if(size.x == 0 && size.y == 0) {
			inst.pack();
			shell.pack();
		} else {
			Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
			shell.setSize(shellBounds.width, shellBounds.height);
		}
		shell.setLocation(display.getClientArea().width / 2 - shell.getSize().x/2, 
				display.getClientArea().height / 2 - shell.getSize().y/2);
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	public SignIn(org.eclipse.swt.widgets.Composite parent, int style) {
		super(parent, style);
		initGUI();
	}

	private void initGUI() {
		try {
			this.setLayout(new FormLayout());
			this.setSize(362, 199);
			
			
			{
				lblName = new Label(this, SWT.NONE);
				FormData lblNameLData = new FormData();
				lblNameLData.width = 47;
				lblNameLData.height = 20;
				lblNameLData.left =  new FormAttachment(178, 1000, 0);
				lblNameLData.top =  new FormAttachment(178, 1000, 0);
				lblNameLData.bottom =  new FormAttachment(278, 1000, 0);
				lblNameLData.right =  new FormAttachment(308, 1000, 0);
				lblName.setLayoutData(lblNameLData);
				lblName.setText("用户ID：");
			}
			{
				FormData txtNameLData = new FormData();
				txtNameLData.width = 132;
				txtNameLData.height = 17;
				txtNameLData.left =  new FormAttachment(352, 1000, 0);
				txtNameLData.right =  new FormAttachment(733, 1000, 0);
				txtNameLData.top =  new FormAttachment(0, 1000, 32);
				txtNameLData.bottom =  new FormAttachment(1000, 1000, -144);
				txtName = new Text(this, SWT.NONE|SWT.BORDER);
				txtName.setLayoutData(txtNameLData);
			}
			
			
			FormData lblPasswdLData;
			{
				lblPasswd = new Label(this, SWT.NONE);
				lblPasswdLData = new FormData();
				lblPasswdLData.width = 47;
				lblPasswdLData.height = 18;
				lblPasswdLData.left =  new FormAttachment(178, 1000, 0);
				lblPasswdLData.top =  new FormAttachment(429, 1000, 0);
				lblPasswd.setLayoutData(lblPasswdLData);
				lblPasswd.setText("密 码：");
			}
			{
				FormData txtPasswdLData = new FormData();
				txtPasswdLData.width = 131;
				txtPasswdLData.height = 17;
				txtPasswdLData.left =  new FormAttachment(352, 1000, 0);
				txtPasswdLData.right =  new FormAttachment(733, 1000, 0);
				txtPasswdLData.top =  new FormAttachment(419, 1000, 0);
				txtPasswd = new Text(this, SWT.PASSWORD|SWT.BORDER);
				lblPasswdLData.bottom = new FormAttachment(txtPasswd, 0, SWT.BOTTOM);
				txtPasswd.setLayoutData(txtPasswdLData);
			}
			
			{
				FormData btnEnterLData = new FormData(56, 24);
				btnEnterLData.width = 56;
				btnEnterLData.height = 24;
				btnEnterLData.left =  new FormAttachment(296, 1000, 0);
				btnEnterLData.top =  new FormAttachment(670, 1000, 0);
				btnEnterLData.bottom =  new FormAttachment(786, 1000, 0);
				btnEnterLData.right =  new FormAttachment(451, 1000, 0);
				btnEnter = new Button(this, SWT.PUSH | SWT.CENTER);
				lblPasswdLData.right = new FormAttachment(btnEnter, -5, SWT.LEFT);
				btnEnter.setLayoutData(btnEnterLData);
				btnEnter.setText("确定");
				btnEnter.setSize(56, 24);
				btnEnter.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent evt) {
						try {
							btnEnterWidgetSelected(evt);
						} catch (IOException e) {
							e.printStackTrace();
						} catch (ClassNotFoundException e) {
							e.printStackTrace();
						}
					}
				});
			}
			{
				FormData btnCancelLData = new FormData(56, 24);
				btnCancelLData.width = 56;
				btnCancelLData.height = 24;
				btnCancelLData.left =  new FormAttachment(570, 1000, 0);
				btnCancelLData.top =  new FormAttachment(670, 1000, 0);
				btnCancelLData.bottom =  new FormAttachment(786, 1000, 0);
				btnCancelLData.right =  new FormAttachment(725, 1000, 0);
				btnCancel = new Button(this, SWT.PUSH | SWT.CENTER);
				btnCancel.setLayoutData(btnCancelLData);
				btnCancel.setText("取消");
				btnCancel.setSize(56, 24);
				btnCancel.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent evt) {
						shell.close();
					}
				});
			}
			
			this.layout();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void btnEnterWidgetSelected(SelectionEvent evt) throws IOException, ClassNotFoundException {
		if(txtName.getText().equals("")){
			box.setMessage("请输入用户ID:");
			box.open();
			return;
		}
		if(txtPasswd.getText().equals("")){
			box.setMessage("请输入密码！");
			box.open();
			return;
		}
		
		// 另外增加的
		if (Util.isValidUserId(txtName.getText().trim()) == false) {
			box.setMessage("用户ID只能由字母、数字、下划线构成，且只能以字母开头!");
			box.open();
			return;
		}
		
		SocketConnection.setCmd("logon");
		ArrayList<Object> out = new ArrayList<Object>();
		out.add(txtName.getText().toString());
		out.add(txtPasswd.getText().toString());
		SocketConnection.setListOut(out);
		try{
		SocketConnection.DataTransmission();
		}
		catch(Exception e)
		{
			System.out.println("服务器连接出错，请检查配置文件和网络连接");
			box.setMessage("服务器连接出错，请检查配置文件和网络连接");
			box.open();
			return;
		}
		
		userType = SocketConnection.getListIn().get(0).toString();
		if(userType.equals("0")){  //systemAdmin
			SystemAdmin.userName = txtName.getText();
			SignIn.display.close();
			SystemAdmin.showGUI();	
		}
		else if(userType.equals("1")){          //ordinaryUser
			CommonUser.userName = txtName.getText();
			SignIn.display.close();
			CommonUser.showGUI();
		}
		else if(userType.equals("2")){             //ReviewUser
			ReviewUser.userName = txtName.getText();
			SignIn.display.close();
			ReviewUser.showGUI();
		}
		else{
			box.setMessage("您的用户名或密码错误");
			box.open();
			return;
		}
	}
}
